package com.matrix.yukun.matrix.selfview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.matrix.yukun.matrix.R;
import com.matrix.yukun.matrix.main_module.utils.ScreenUtil;
import com.matrix.yukun.matrix.main_module.utils.ScreenUtils;
import com.matrix.yukun.matrix.util.log.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator
 * 日期：2023/10/12 10:40
 * 描述：
 *
 * @author Administrator
 */
public class BatteryLinearLayout extends LinearLayout {

    private TextView tvLayout;
    List<View> mList = new ArrayList<>();
    private LinearLayout mLlContain;
    private int max;

    public BatteryLinearLayout(Context context) {
        super(context);
        initView(context, null, 0);

    }

    public BatteryLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs, 0);
    }

    public BatteryLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void initView(Context context, AttributeSet attrs, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.battery_ll_layout, null);
        tvLayout = view.findViewById(R.id.tv_layout);
        mLlContain = view.findViewById(R.id.ll_contain);
        initData(mLlContain);
        ViewGroup.LayoutParams layoutParams = tvLayout.getLayoutParams();
        layoutParams.width = ScreenUtil.screenWidth;
        tvLayout.setLayoutParams(layoutParams);
        this.addView(view);
    }

    private void initData(LinearLayout llContain) {
        for (int i = 0; i < llContain.getChildCount() - 1; i++) {
            mList.add(llContain.getChildAt(i));
        }
    }

    public void setMax(int max) {
        this.max = max * 100;
    }

    /**
     * 设置数值
     *
     * @param progress
     */
    public  void setProgress(double progress) {
        if (max == 0) {
            return;
        }
        progress = progress * 100;
        //个数
        int count = mList.size();
        //每个最大值
        int progressMax = max / count;
        //满值的个数
        int position = (int) (progress / progressMax);
        //余数
        int currentPro = (int) (progress % progressMax);
//        LogUtil.i("==========progressMax：" + progressMax);
//        LogUtil.i("==========progress：" + progress);
//        LogUtil.i("==========currentPro：" + currentPro);
        //相同,全满值
        for (int i = 0; i < mList.size(); i++) {
            ((ProgressBar) mList.get(i)).setProgress(0);
        }
        if (progress == max || progress > max) {
            for (int i = 0; i < mList.size(); i++) {
                ((ProgressBar) mList.get(i)).setMax(progressMax);
                ((ProgressBar) mList.get(i)).setProgress(progressMax);
            }
            return;
        }
        //分配每个view
        for (int i = 0; i < mList.size(); i++) {
            ((ProgressBar) mList.get(i)).setMax(progressMax);
            if (i < position) {
                ((ProgressBar) mList.get(i)).setProgress(progressMax);
            } else if (i == position && i != mList.size()) {
                ((ProgressBar) mList.get(i)).setProgress(currentPro);
            }
        }
    }

}
